<?php

namespace Bean\Flow\PostsFlow;

abstract class Item extends \Bean\Flow\GenericFlow\Item {

	private $_data;
	private $_time;
	private $_flow;
	private $_comments;
	private $_links;

	public function __construct( $data, \Bean\Flow\PostsFlow $flow ) {
		parent::__construct();
		$this->_data = $data;
		$this->_flow = $flow;
	}
	
	public static function getItem( $post, \Bean\Flow\PostsFlow $flow ) {
		$class = get_class() . '\\' . $post['type'] . 'Post';
		$instance = new $class( $post, $flow );
		return $instance;
	}
	
	public static function get( $post_id, \Bean\Flow\PostsFlow $flow = null ) {
		$post = _q()->get1( 'posts', array( 'post_id' => $post_id ) );
		if( $flow === null ) {
			$flow = \Bean\Flow\PostsFlow::get( $post['flow_id'] );
		}
		return self::getItem( $post, $flow );
	}
	
	public function isViewable( \Bean\User $user = null ) {
		return true;
	}
	
	public function isDeletable( \Bean\User $user = null ) {
	
		if( $user === null ) {
			$user = _u();
		}
		if( !$user ) {
			return false;
		}
		$flow = $this->getFlow();
		
		return $flow->getUser()->is( $user );
	}
	
	public function getMessage() {
		return $this->_data['message'];
	}
	
	public function getType() {
		return $this->_data['type'];
	}

	public function getFlow() {
		return $this->_flow;
	}
	
	public function getPublicationTime() {
		if( $this->_time === null ) {
			$time = strtotime( $this->_data['publication_time'] );
			$this->_time = $time;
		}
		return $this->_time;
	}
	
	public function getPostId() {
		return $this->_data['post_id'];
	}
	
	public function getItemId() {
		return $this->getPostId();
	}
	
	public function isLikedBy( \Bean\User $user ) {
		$user = _u($user);
		if( !$user ) {
			return false;
		}
		return _q()->getC( 'posts_likes', array(
			'user_id' => $user->getUserId(),
			'post_id' => $this->getPostId()
		) ) ? true : false;
	}
	
	public function like( \Bean\User $user = null ) {
		$user = _u($user);
		if( !$user ) {
			return $this;
		}
		_q()->set1(
			'posts_likes',
			array( 
				'user_id' => $user->getUserId(),
				'post_id' => $this->getPostId()
			) );
		return $this;
	}
	
	public function unlike( \Bean\User $user = null ) {
		$user = _u($user);
		if( !$user ) {
			return $this;
		}
		_q()->del(
			'posts_likes',
			array( 
				'user_id' => $user->getUserId(),
				'post_id' => $this->getPostId()
			) );
		return $this;
	}
	
	public function getLikes() {
	
		$n = _q()->getC( 'posts_likes', array( 'post_id' => $this->getPostId() ) );

		switch( $n ) {
			case 0 :
				return false;
			case 1 :
				return '1 person likes this';
			default :
				return $n . ' people like this.';
		}
	}
	
	public function getComments() {
		
		if( $this->_comments === null ) {
			$results = _q()->getE( 'posts_comments',
				array( 'post_id' => $this->getPostId() ),
				'publication_time' );

			$comments = array();
			foreach( $results as $comment ) {
				$comments[] = new Comment( $comment, $this );
			}
			$this->_comments = $comments;
		}
		
		return $this->_comments;
	}

	public function addComment( $message ) {
		$now = _now();
		$comment_id = _q()->set1( 'posts_comments',
			array(
				'user_id' => _u()->getUserId(),
				'post_id' => $this->getPostId(),
				'message' => $message,
				'publication_time' => $now
			) );

		return new Comment( array( 
			'post_id' => $this->getPostId(),
			'comment_id' => $comment_id,
			'message' => $message,
			'publication_time' => $now,
			'user_id' => _u()->getUserId()
		), $this );
	}

	public function getLinks() {
		if( $this->_links === null ) {
			$this->_links = array();
			$links = _q()->getN( 'posts_links', array( 'post_id' => $this->getPostId() ) );
			foreach( $links as $link ) {
				$this->_links[] = new Link( $link );
			}
		}
		return $this->_links;
	}
	
	public function addLink( $data ) {
	
		$data['user_id'] = _u()->getUserId();
		$data = array( 'data_link_id' => _q()->set1( 'data_links', $data ) );
		$data['post_id'] = $this->getPostId();
		$data['link_id'] = _q()->set1( 'posts_links', $data );

		return new Link( $data );
	}
	
	public function delete() {
		foreach( $this->getComments() as $comment ) {
			$comment->delete();
		}
		_q()->del( 'posts_likes',
			array(
				'post_id' => $this->getPostId()
			) );
		_q()->del( 'posts_links',
			array(
				'post_id' => $this->getPostId()
			) );
		_q()->del( 'posts',
			array(
				'post_id' => $this->getPostId()
			) );
		
		return $this;
	}
}
